package pro.seac.twitter.web.view;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.replace;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pro.seac.twitter.application.MessageService;
import pro.seac.twitter.application.UserManagementService;
import pro.seac.twitter.application.dto.UserViewEntry;
import pro.seac.twitter.web.Navigateable;
import pro.seac.twitter.web.view.component.MessageList;
import pro.seac.twitter.web.view.component.UserTable;

import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

@Component
@Scope("prototype")
@Navigateable("users/**")
public class UserProfileView extends BaseView implements TabSheet.SelectedTabChangeListener {

	private static final long serialVersionUID = 1L;
	private UserViewEntry model;
	@Autowired
	private UserManagementService service;
	@Autowired
	private MessageService messageService;
	private UserTable followedTable, followersTable;
	private MessageList messagesList;

	@Override
	public void enter(ViewChangeEvent event) {
		String username = replace(event.getViewName(), "users/", EMPTY);
		model = service.getUserProfileData(username);
		initComponents();
		refreshMessages();
	}

	private VerticalLayout profileContainer;
	private TabSheet tabSheet;

	@Override
	protected com.vaadin.ui.Component initContent() {
		profileContainer = (VerticalLayout) super.initContent();
		profileContainer.setSizeFull();

		Button button;
		if (getCurrentUser() != null && getCurrentUserId() != model.getId()) {
			profileContainer.addComponent(button = new Button("obserwuj"));
			button.addClickListener(new ClickListener() {
				private static final long serialVersionUID = 1L;

				@Override
				public void buttonClick(ClickEvent event) {
					startFollowingUser();
				}
			});
		}

		tabSheet = new TabSheet();
		tabSheet.setSizeFull();

		//
		VerticalLayout container = (VerticalLayout) super.initContent();
		container.setSizeFull();
		container.setMargin(true);
		Panel messagesContainer = new Panel();
		messagesContainer.setStyleName(Reindeer.PANEL_LIGHT);
		messagesContainer.setSizeFull();
		messagesList = new MessageList();
		messagesContainer.setContent(messagesList);
		container.addComponent(messagesContainer);
		container.setExpandRatio(messagesContainer, 1);

		tabSheet.addTab(container, "Wiadomości", null, 0);

		tabSheet.addTab(followedTable = new UserTable(), "Obserwowani", null, 1);
		followedTable.setSizeFull();
		tabSheet.addTab(followersTable = new UserTable(), "Obserwujący", null, 2);
		followersTable.setSizeFull();

		tabSheet.addSelectedTabChangeListener(this);
		profileContainer.addComponent(tabSheet);
		profileContainer.setExpandRatio(tabSheet, 1);
		return profileContainer;
	}

	public void selectedTabChange(SelectedTabChangeEvent event) {
		TabSheet tabsheet = event.getTabSheet();
		Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
		int position = tabsheet.getTabPosition(tab);
		if (position == 0) {
			refreshMessages();
		} else if (position == 1) {
			followedTable.refreshUsers(service.getFollowed(model.getId()));
		} else if (position == 2) {
			followersTable.refreshUsers(service.getFollowers(model.getId()));
		}
	}

	@Override
	protected String getTitle() {
		return "Profil u\u017Cytkownika: " + model.getUserName();
	}

	public void startFollowingUser() {
		try {
			service.startFollowingUser(getCurrentUserId(), model.getId());
			Notification.show("Sukces", "Obserwujesz u\u017Cytkownika: " + model.getName(), Type.HUMANIZED_MESSAGE);
		} catch (Exception e) {
			Notification.show("B\u0142\u0105d", "Wyst\u0105pi\u0142 b\u0142\u0105d: " + e.getMessage() + "!", Type.ERROR_MESSAGE);
		}
	}

	public void refreshMessages() {
		messagesList.refreshMessages(messageService.getUserProfileFeedMessages(model.getId()));
	}
}
